package com.googlecode.bip.xml.importe;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
import com.googlecode.bip.dao.mysql.MySQLDAOFactory;
import cx.ath.choisnet.sql.ConnectionQuery;

/**
 *
 * @author Claude CHOISNET
 */
public abstract class Commiter implements Commitable
{
    private static final Logger logger = Logger.getLogger( Commiter.class );

    /**
     * Must call {@link #performeCommit(String)}
     * @throws SQLException if any
     * @throws NamingException if any
     */
    @Override
    public abstract void commit()
        throws NamingException, SQLException;

    /**
     * Perform SQL update in database.
     *
     * @param sqlUpdate SQL to send to database.
     * @throws NamingException if any
     * @throws SQLException if any
     */
    protected final void performeCommit( final String sqlUpdate )
        throws NamingException, SQLException
    {
        logger.debug( "SQL: " + sqlUpdate );

        Connection c = getConnection();
        try {
            ConnectionQuery connectionQuery = new ConnectionQuery( c ) ;

            connectionQuery.doUpdate( sqlUpdate );
            }
        finally {
            c.close();
            }
    }

    private Connection getConnection()
        throws NamingException, SQLException
    {
        return MySQLDAOFactory.getConnection();
    }

}
